﻿namespace ManagerClinic.CanLamSang
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Linq;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using System.Configuration;
    using ManagerClinic.Ultilites.HtmlEditor.Buttons;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmPhieuXetNghiem : DevExpress.XtraEditors.XtraForm
    {
        string _MaXetNghiem = string.Empty, _MaHoSoKhamBenh = string.Empty, _MaDichVu = string.Empty;
        Int32 _status;
        bool resultInsert = false;
        public event EventHandler _SavePhieuCanLamSang = null;

        HoSoKhamBenhNgoaiTruEntity hoSoKhamBenhNgoaiTruEntity = null;
        PhieuXetNghiemEntity phieuXetNghiemEntity = null;
        IList<PhieuXetNghiemChiTietEntity> lstPhieuXetNghiemChiTietEntity = null;

        ThuPhiDichVuBusiness thuPhiDichVuBusiness = new ThuPhiDichVuBusiness();
        PhieuXetNghiemBusiness phieuXetNghiemBusiness = new PhieuXetNghiemBusiness();
        PhieuXetNghiemChiTietBusiness phieuXetNghiemChiTietBusiness = new PhieuXetNghiemChiTietBusiness();
        DmBacSyBusiness dmBacSyBusiness = new DmBacSyBusiness();
        HoSoKhamBenhNgoaiTruBusiness hoSoKhamBenhNgoaiTruBusiness = new HoSoKhamBenhNgoaiTruBusiness();

        public frmPhieuXetNghiem(Int32 status, string maDichVu, HoSoKhamBenhNgoaiTruEntity obj)
        {
            InitializeComponent();

            _status = status;
            resultInsert = false;
            hoSoKhamBenhNgoaiTruEntity = obj;
            _MaDichVu = maDichVu;
            PredefinedButtonSets.SetupDefaultButtons(this.txtKetLuan);
            _MaXetNghiem = ConstantManagerClinic.setCodeManagerClinic(ConstantManagerClinic.maPhieuXetNghiem);

            ucThongTinHanhChinh1.LoadThongTinHanhChinh(hoSoKhamBenhNgoaiTruEntity.MaBenhNhan);
            ucChiDinhKhoaPhong.LoadDmKhoaKham();
            ucChiDinhKhoaPhong.LoadDmKhoaCanLamSang();
            ucChiDinhKhoaPhong.LoadDmPhongKham();
            ucChiDinhKhoaPhong.LoadDmPhongCanLamSang();
            ucChiDinhKhoaPhong.LoadDmDichVuChiTiet(_MaDichVu);
        }

        public frmPhieuXetNghiem(Int32 status, string maXetnghiem)
        {
            InitializeComponent();

            PredefinedButtonSets.SetupDefaultButtons(this.txtKetLuan);
            _status = status;
            resultInsert = true;
            _MaXetNghiem = maXetnghiem;

            phieuXetNghiemEntity = phieuXetNghiemBusiness.GetPhieuXetNghiem(_MaXetNghiem);
            hoSoKhamBenhNgoaiTruEntity = hoSoKhamBenhNgoaiTruBusiness.GetHoSoKhamBenhNgoaiTru(phieuXetNghiemEntity.MaHoSo);
            _MaDichVu = phieuXetNghiemEntity.MaDichVu;

            ucThongTinHanhChinh1.LoadThongTinHanhChinh(phieuXetNghiemEntity.MaBenhNhan);
            ucChiDinhKhoaPhong.LoadDmKhoaKham();
            ucChiDinhKhoaPhong.LoadDmKhoaCanLamSang();
            ucChiDinhKhoaPhong.LoadDmPhongKham();
            ucChiDinhKhoaPhong.LoadDmPhongCanLamSang();
            ucChiDinhKhoaPhong.setMaKhoaKham(phieuXetNghiemEntity.MaKhoaKham);
            ucChiDinhKhoaPhong.setMaPhongKhamBenh(phieuXetNghiemEntity.MaPhongKham);
            ucChiDinhKhoaPhong.setMaKhoaCanLamSang(phieuXetNghiemEntity.MaKhoaCanLamSang);
            ucChiDinhKhoaPhong.setMaPhongCanLamSang(phieuXetNghiemEntity.MaPhongCanLamSang);
            ucChiDinhKhoaPhong.LoadDmDichVuChiTiet(_MaDichVu);

            LoadInfoPhieu(phieuXetNghiemEntity);
            LoadDataResult(_MaXetNghiem);
        }

        void LoadInfoPhieu(PhieuXetNghiemEntity obj)
        {
            ucChiDinhKhoaPhong.setMaKhoaKham(obj.MaKhoaKham);
            ucChiDinhKhoaPhong.setMaPhongKhamBenh(obj.MaPhongKham);
            ucChiDinhKhoaPhong.setMaKhoaCanLamSang(obj.MaKhoaCanLamSang);
            ucChiDinhKhoaPhong.setMaPhongCanLamSang(obj.MaPhongCanLamSang);
            ucChiDinhKhoaPhong.setBacSy(obj.MaBacSyKham);

            txtKetLuan.Html = obj.KetLuan;

            var dmBacSyClsEntity = dmBacSyBusiness.GetDmBacSy(obj.MaBacSyCls);
            if (dmBacSyClsEntity != null)
                txtBacSyCls.Text = dmBacSyClsEntity.MaBacSy + " - " + dmBacSyClsEntity.TenBacSy;
        }

        void LoadDataResult(string maXetNghiem)
        {
            lstPhieuXetNghiemChiTietEntity = phieuXetNghiemChiTietBusiness.GetPhieuXetNghiemChiTietByMaXetNghiemList(maXetNghiem);
            grdKetqua.DataSource = lstPhieuXetNghiemChiTietEntity;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            ucChiDinhKhoaPhong.IsValidLookupEdit();
            ucChiDinhKhoaPhong.IsValidChooseDichVuChiTiet();

            if (resultInsert == false)
            {
                phieuXetNghiemEntity = new PhieuXetNghiemEntity()
                {
                    MaXetNghiem = _MaXetNghiem,
                    Stt = 0,
                    SoVaoVien = hoSoKhamBenhNgoaiTruEntity.SoVaoVien,
                    MaHoSo = hoSoKhamBenhNgoaiTruEntity.MaHoSo,
                    NgayVaoKham = hoSoKhamBenhNgoaiTruEntity.NgayVaoKham,
                    YeuCau = ucChiDinhKhoaPhong.getYeuCau(),
                    GhiChu = ucChiDinhKhoaPhong.getGhiChu(),
                    KetLuan = txtKetLuan.Html.Trim(),
                    MaBacSyCls = string.Empty,
                    MaBacSyKham = Authenticate.MaBacSy,
                    TrangThaiPhieu = (int)ConstantManagerClinic.TrangThaiPhieuCanLamSang.TaoMoi,
                    MaKhoaCanLamSang = ucChiDinhKhoaPhong.getMaKhoaCanLamSang(),
                    MaPhongCanLamSang = ucChiDinhKhoaPhong.getMaPhongCanLamSang(),
                    MaDichVu = _MaDichVu,
                    MaKhoaKham = ucChiDinhKhoaPhong.getMaKhoaKham(),
                    MaPhongKham = ucChiDinhKhoaPhong.getMaPhongKham(),
                    TrangThaiCho = Convert.ToBoolean(ConstantManagerClinic.TrangThaiChoDichVuKhamChuaBenh.ChuaThucHien)
                };
                resultInsert = phieuXetNghiemBusiness.InsertPhieuXetNghiem(phieuXetNghiemEntity);
            }
            else
            {
                phieuXetNghiemEntity = phieuXetNghiemBusiness.GetPhieuXetNghiem(_MaXetNghiem);
                phieuXetNghiemEntity.YeuCau = ucChiDinhKhoaPhong.getYeuCau();
                phieuXetNghiemEntity.GhiChu = ucChiDinhKhoaPhong.getGhiChu();
                phieuXetNghiemEntity.KetLuan = txtKetLuan.Html.Trim();
                phieuXetNghiemEntity.MaKhoaCanLamSang = ucChiDinhKhoaPhong.getMaKhoaCanLamSang();
                phieuXetNghiemEntity.MaPhongCanLamSang = ucChiDinhKhoaPhong.getMaPhongCanLamSang();
                phieuXetNghiemEntity.MaDichVu = _MaDichVu;
                phieuXetNghiemEntity.MaKhoaKham = ucChiDinhKhoaPhong.getMaKhoaKham();
                phieuXetNghiemEntity.MaPhongKham = ucChiDinhKhoaPhong.getMaPhongKham();

                phieuXetNghiemBusiness.EditPhieuXetNghiem(phieuXetNghiemEntity);
            }

            //delete from table ThuPhiDichVu
            thuPhiDichVuBusiness.DeleteThuPhiDichVuByMaPhieuDichVu(_MaXetNghiem);
            //delete from table PhieuXetNghiemChiTiet
            phieuXetNghiemChiTietBusiness.DeletePhieuXetNghiemChiTiet_ByMaPhieuXetNghiem(_MaXetNghiem);

            if (resultInsert == true)
            {
                //Insert into table ThuPhiDichVu, PhieuSieuAmChiTiet
                IList<DmDichVuChiTietEntity> lstDmDichVuChiTietIsChecked = ucChiDinhKhoaPhong.DmDichVuChiTietEntityIsChecked;
                if (lstDmDichVuChiTietIsChecked == null) { return; }

                foreach (DmDichVuChiTietEntity dmDichVuChiTietEntity in lstDmDichVuChiTietIsChecked)
                {
                    var thuphiDichVuEntity = new ThuPhiDichVuEntity()
                    {
                        MaPhieuDichVu = _MaXetNghiem,
                        MaHoSo = phieuXetNghiemEntity.MaHoSo,
                        NgayDenKham = phieuXetNghiemEntity.NgayVaoKham,
                        MaDichVu = _MaDichVu,
                        MaChiTieu = dmDichVuChiTietEntity.MaChiTieu,
                        TongTien = dmDichVuChiTietEntity.DonGia,
                        DaDongTien = (int)ConstantManagerClinic.TrangThaiDongTien.ChuaDongTien,
                        MaNguoiDung = Authenticate.UserName,
                        MaKhoaKham = phieuXetNghiemEntity.MaKhoaKham,
                        MaPhongKham = phieuXetNghiemEntity.MaPhongKham,
                        MaKhoaCanLamSang = phieuXetNghiemEntity.MaKhoaCanLamSang,
                        MaPhongCanLamSang = phieuXetNghiemEntity.MaPhongCanLamSang,
                        DaThucHien = Convert.ToBoolean(ConstantManagerClinic.TrangThaiThucHienDichVuKhamChuaBenh.ChuaThucHien)
                    };
                    thuPhiDichVuBusiness.InsertThuPhiDichVu(thuphiDichVuEntity);

                    var phieuXetNghiemChiTietEntity = new PhieuXetNghiemChiTietEntity()
                    {
                        MaXetNghiem = _MaXetNghiem,
                        MaChiTieu = dmDichVuChiTietEntity.MaChiTieu,
                        KetQua = "",
                        DonGia = dmDichVuChiTietEntity.DonGia,
                        DaThuTien = (int)ConstantManagerClinic.TrangThaiDongTien.ChuaDongTien
                    };
                    phieuXetNghiemChiTietBusiness.InsertPhieuXetNghiemChiTiet(phieuXetNghiemChiTietEntity);
                }

                LoadDataResult(_MaXetNghiem);
            }

            if (_SavePhieuCanLamSang != null)
                _SavePhieuCanLamSang(sender, e);
        }

        private void btnLuuKetQua_Click(object sender, EventArgs e)
        {
            //Luu ket qua phieu xet nghiem
            phieuXetNghiemEntity = phieuXetNghiemBusiness.GetPhieuXetNghiem(_MaXetNghiem);
            phieuXetNghiemEntity.KetLuan = txtKetLuan.Html;
            phieuXetNghiemEntity.MaBacSyCls = Authenticate.MaBacSy;
            phieuXetNghiemBusiness.EditPhieuXetNghiem(phieuXetNghiemEntity);
            //Luu ket qua phieu xet nghiem chi tiet
            //TODO:...

            MessageBox.Show("Lưu kết quả thành công", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDongKetQua_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmPhieuXetNghiem_Load(object sender, EventArgs e)
        {
            ucChiDinhKhoaPhong.setDmDichVuChiTietEntityIsChecked(_MaXetNghiem, _MaDichVu);
        }

    }
}